﻿using Sustainalytics.ClientDW.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Sustainalytics.Claims;
using System.Web.Http.Cors;

namespace Sustainalytics.GCCS.Service.Controllers
{
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    //[AutoInvalidateCacheOutput]
    [RoutePrefix("api/v1/gcprinciplesummary")]
    public class GCPrincipleSummaryController : ApiController
    {
        IGCPrincipleSummaryManager _manager;
        IClaimsChecker _claimsChecker;

        public GCPrincipleSummaryController(IGCPrincipleSummaryManager manager, IClaimsChecker claimsChecker)
        {
            _manager = manager;
            _claimsChecker = claimsChecker;
        }


       // [CacheOutput(ServerTimeSpan = 100, ClientTimeSpan = 0, NoCache = true, MustRevalidate = true, Private = true)]
        [Route("")]
        public HttpResponseMessage Get(Guid companyId, GCAssessment? overallAssessment, bool fetchCount = false)
        {
            if (!_claimsChecker.ValidateNonCompliantOrWatchlist(this))
            {
                return this.Request.CreateResponse(HttpStatusCode.Unauthorized);
            }

            if (fetchCount == false)
            {
                // get company principles
                List<GCPrincipleSummary> lst = _manager.GetList(companyId, overallAssessment);

                // project to DTOs
                List<GCPrincipleSummaryDto> dtos = lst.Select(el => new GCPrincipleSummaryDto().LoadFrom(el)).ToList();

                return this.Request.CreateResponse(HttpStatusCode.OK, dtos);
            }
            else
            {
                // get principles count
                int count = _manager.GetCount(companyId, overallAssessment);

                return this.Request.CreateResponse(HttpStatusCode.OK, count);
            }
        }
    }
}
